<?php
	session_start();
	require('../Gen_Scripts/gen_functions.php');
	login();
	wipe_active();
	
	$query=
	"select t.game_no, t.game_day, t.close_poll_dt
	   from TOURNY t
	  where t.t_id = ".max_tid();
	if($result = mysql_query($query)){
		if($row = mysql_fetch_array($result)){
			$lst_game_no = $row['game_no'];
			$lst_game_day = $row['game_day'];
			$lst_close_poll = $row['close_poll_dt'];
		}
	}
	
	if($_POST['add_tourny'] == 'submit'){
		$poster =  $_POST['poster'];
		$post_dt =  $_POST['post_dt'];
		$vote =  $_POST['vote'];
		$lst_game_no =  $_POST['lst_game_no'];
		$game_no =  $_POST['game_no'];
		$lst_game_day =  $_POST['lst_game_day'];
		$game_day =  $_POST['game_day'];
		$lst_close_poll =  $_POST['lst_close_poll'];
		$close_poll =  $_POST['close_poll'];
		$game_a =  $_POST['game_a'];
		$gp_a =  $_POST['gp_a'];
		$game_b =  $_POST['game_b'];
		$gp_b =  $_POST['gp_b'];
		$post =  $_POST['post'];
		
		$add=
		"insert into TOURNY (GAME_DAY, GAME_NO, GAMEA_ID, GAMEA_GP, 
									     GAMEB_ID, GAMEB_GP,POST, POSTER, POST_DT, CLOSE_POLL_DT,VOTE) 
		values ('$game_day', $game_no, $game_a, '$gp_a', $game_b, '$gp_b', '$post', $poster, $post_dt, '$close_poll', '$vote' )";
		if($result = mysql_query($add)){
			echo 'Query Sucessful! <br>';
		}else{
			echo 'Try again! <br>';
		}
	}
	
?>
<script type="text/javascript">
$(document).ready(function(){
    // bind form using ajaxForm 
    $('#TADD').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        target: '#t_add_ajax', 
 
        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function() { 
            $('#t_add_ajax').fadeIn('slow'); 
        }
    }); 
});
</script>


<form id="TADD" method="post" action="Manage/t_add.php">
	<input type="hidden" name="poster" value="<?php echo $_SESSION['USERNUM']; ?>">
	<input type="hidden" name="post_dt" value="now()">
	<input type="hidden" name="vote" value="?">

	<div id="t1">
		<h1> Last &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; New </h1>
		<label>Game Number:</label>
		<input name="lst_game_no"    type="text" value="<?php echo $lst_game_no; ?>" />
		<input name="game_no"        type="text" value="<?php echo $game_no; ?>" />
		<label>Game Day:</label>
		<input name="lst_game_day"   type="text" value="<?php echo $lst_game_day; ?>" />
		<input name="game_day"       type="text" value="<?php echo $game_day; ?>" />
		<label>Close Poll:</label>
		<input name="lst_close_poll" type="text" value="<?php echo $lst_close_poll; ?>" />
		<input name="close_poll"     type="text" value="<?php echo $close_poll; ?>" />
	</div>
	
	<div id="games">
		<div id="gamea">
			<div id="title">
				<label>Game A </label>
				<select NAME="game_a">
				<?php 
					dblogin();
					$lista = "
						select gameid, game, owner
						from GAMES g";
					if($result = mysql_query($lista)){
						while($row = mysql_fetch_array($result)){
							echo '<option value="'.$row['gameid'].'"> '.$row['game'].'|'.player_name($row['owner']);
						}
					}
				?>
				</select>
			</div>
			<textarea name="gp_a"></textarea>
		</div>
		
		<div id="gameb">
			<div id="title">
				<label>Game B </label>
				<select NAME="game_b">
				<?php 
					dblogin();
					$lista = "
						select gameid, game, owner
						from GAMES g";
					if($result = mysql_query($lista)){
						while($row = mysql_fetch_array($result)){
							echo '<option value="'.$row['gameid'].'"> '.$row['game'].'|'.player_name($row['owner']);
						}
					}
				?>
				</select>
			</div>
			<textarea name="gp_b"></textarea>
		</div>
	</div>

	<textarea class="post" name="post"></textarea>
	
	
	<div id="buttons" class="edit">
		<input type="submit" value="submit" name="add_tourny" />
	</div>
</form>
